import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  Delete,
  UseGuards,
  Req,
} from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { QueryMenuDto } from './dto/query-menu.dto';
import { MenuPipe } from './pipe/menu.pipe';
import { Request } from 'express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { User } from '../user/entities/user.entity';
import { RequirePermission } from 'src/decorator/reuqire-permission';

@ApiTags('菜单管理')
@Controller('menu')
@ApiBearerAuth()
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @Post()
  @ApiOperation({ summary: '创建菜单' })
  @RequirePermission("system:menu:add")
  create(
    @Body(MenuPipe) createMenuDto: CreateMenuDto,
    @Req() request: Request,
  ) {
    return this.menuService.create(createMenuDto, request);
  }

  @Get()
  @ApiOperation({ summary: '获取菜单列表' })
  @ApiResponse({
    status: 200,
    description: '获取菜单列表',
  })
  @RequirePermission("system:menu:list")
  findAll(@Req() req:Request,@Query() query: QueryMenuDto) {
    let exitUser = req.user as User;
    return this.menuService.findAll(exitUser.id,query);
  }

  @Get(':id')
  @ApiOperation({ summary: '根据ID获取单个菜单' })
  findOne(@Param('id') id: string) {
    return this.menuService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '根据ID更新菜单' })
  @RequirePermission("system:menu:edit")
  update(
    @Param('id') id: string,
    @Body() updateMenuDto: UpdateMenuDto,
    @Req() request: Request,
  ) {
    return this.menuService.update(+id, updateMenuDto, request);
  }

  @Delete(':id')
  @ApiOperation({ summary: '根据ID删除菜单' })
  @RequirePermission("system:menu:remove")
  remove(@Param('id') id: string) {
    return this.menuService.remove(+id);
  }
}
